// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <pollux/vector/complex_vector.h>

namespace kumo::pollux::exec {
    class ExprSet;
}

namespace kumo::pollux::core {
    class ITypedExpr;

    // Exposes expression evaluation functionality of the engine to other parts of
    // the code base.  Connector may use it, for example, to evaluate pushed down
    // filters.  This is not thread safe and serializing operations is the
    // responsibility of the caller.  This is self-contained and does not reference
    // objects from the thread which constructs this.  Passing this between threads
    // is allowed as long as uses are sequential.  May reference query-level
    // structures like QueryCtx.
    class ExpressionEvaluator {
    public:
        virtual ~ExpressionEvaluator() = default;

        // Compiles an expression. Returns an instance of exec::ExprSet that can be
        // used to evaluate that expression on multiple vectors using evaluate method.
        virtual std::unique_ptr<exec::ExprSet> compile(
            const std::shared_ptr<const ITypedExpr> &expression) = 0;

        // Evaluates previously compiled expression on the specified rows.
        // Re-uses result vector if it is not null.
        virtual void evaluate(
            exec::ExprSet *exprSet,
            const SelectivityVector &rows,
            const RowVector &input,
            VectorPtr &result) = 0;

        // Memory pool used to construct input or output vectors.
        virtual memory::MemoryPool *pool() = 0;
    };
} // namespace kumo::pollux::core
